﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GazerView.xaml.cs" company="R.V.Kasner">
//   Copyright © 2011 R.V.Kasner
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Owen.Terminal.View
{
    using System;
    using System.Windows;

    using FontDialogSample;

    using Owen.Terminal.ViewModel;

    public partial class GazerView : IGazerView
    {
        public GazerView()
        {
            this.InitializeComponent();
        }

        public event Action<bool> ExpanderChange;

        public IGazerViewModel ViewModel
        {
            get
            {
                return this.DataContext as IGazerViewModel;
            }

            set
            {
                if (this.DataContext == value)
                {
                    return;
                }

                this.DataContext = value;
            }
        }

        IViewModel IView.ViewModel
        {
            get
            {
                return this.ViewModel;
            }

            set
            {
                this.ViewModel = value as IGazerViewModel;
            }
        }

        private void RiseExpanderChange(bool isExpand)
        {
            Action<bool> handler = this.ExpanderChange;
            if (handler != null)
            {
                handler(isExpand);
            }
        }

        private void OnFontButtonClick(object sender, RoutedEventArgs e)
        {
            this.ShowFontDialog();
        }

        private void ShowFontDialog()
        {
            var fontChooser = new FontChooser { Owner = Application.Current.MainWindow };

            fontChooser.SetPropertiesFromObject(this.logTextBox);
            fontChooser.PreviewSampleText = this.logTextBox.SelectedText;

            if (fontChooser.ShowDialog().Value)
            {
                fontChooser.ApplyPropertiesToObject(this.logTextBox);
            }
        }

        private void OnLogTextBoxTextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            logTextBox.ScrollToEnd();
        }

        private void OnExpanderExpanded(object sender, RoutedEventArgs e)
        {
            this.RiseExpanderChange(true);
        }

        private void OnExpanderCollapsed(object sender, RoutedEventArgs e)
        {
            this.RiseExpanderChange(false);
        }
    }
}